package com.example.iotlink.service;

import com.example.iotlink.service.impl.*;
import org.eclipse.paho.client.mqttv3.MqttTopic;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.Map;

@Component
public class ReceiptHandleStrategyFactory {

    private static Map<String, ITopicHandleStrategy> topicHandleStrategyMap;

    private ReceiptHandleStrategyFactory() {
        topicHandleStrategyMap = new HashMap<>();
        topicHandleStrategyMap.put("bios/+/event/realdata/post", new RealDataReceiptHandleStrategy());
        topicHandleStrategyMap.put("bios/+/event/alarm/upper/post", new AlarmUpperReceiptHandleStrategy());
        topicHandleStrategyMap.put("bios/+/event/alarm/lower/post", new AlarmLowerReceiptHandleStrategy());
        topicHandleStrategyMap.put("bios/+/event/sync_data/post/response", new SyncReceiptHandleStrategy());
        topicHandleStrategyMap.put("bios/+/event/warning_status/post", new WarnStatReceiptHandleStrategy());
    }

    public static ITopicHandleStrategy getReceiptHandleStrategy(String receiptTopic) {
        for (String key : topicHandleStrategyMap.keySet()) {
            if (MqttTopic.isMatched(key, receiptTopic)) {
                return topicHandleStrategyMap.get(key);
            }
        }
        return null;
    }
}
